package edu.cs.usask.ca.silver.businesslogic.commands.generic;

import edu.cs.usask.ca.silver.api.businesslogic.ParameterI;
import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.ids.ParameterID;
import edu.cs.usask.ca.silver.businesslogic.ids.ParameterSetID;
import edu.cs.usask.ca.silver.businesslogic.ids.ScenarioID;
import edu.cs.usask.ca.silver.businesslogic.util.SilverAutoManagedObjectFactory;
import edu.cs.usask.ca.silver.businesslogic.util.SilverBLTransactionManager;

/**
 * Generic Parameter implementation
 * 
 * @author Yudi Xue
 * 
 */
public class Parameter implements ParameterI {

	private static final SilverAutoManagedObjectFactory parameterFactory = new SilverAutoManagedObjectFactory(
			"edu.cs.usask.ca.silver.businesslogic.commands.generic.Parameter");

	public static SilverAutoManagedObjectFactory getObjectPoolFactory() {
		return parameterFactory;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#addParameter(java
	 * .lang.String, java.lang.String, java.lang.String, java.lang.String,
	 * java.lang.String,
	 * edu.cs.usask.ca.silver.businesslogic.ids.ParameterSetID,
	 * java.lang.String, java.lang.String)
	 */
	public ParameterID addParameter(String name, String units, String type,
			String associatedClass, String descr, ParameterSetID psId,
			String pValue, String pValComment, StorageI storage) throws Exception {

		boolean error = false;
		ParameterID result;

		try {
			SilverBLTransactionManager.startTransaction();
			int pIdInt = storage
					.addParameter(name, units, type, associatedClass, descr,
							psId.getIdVal(), pValue, pValComment);
			result = new ParameterID(pIdInt);
		} catch (Exception e) {
			error = true;
			throw e;
		} finally {
			try {
				SilverBLTransactionManager.closeTransaction(error);
			} catch (Exception e) {
				throw e;
			}
		}

		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#getParameterAsXML
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ParameterID)
	 */
	public String getParameterAsXML(ParameterID pId, StorageI storage) throws Exception {
		int pIdInt = pId.getIdVal();
		String result = storage.getParameterAsXML(pIdInt);
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#getName(edu.cs.usask
	 * .ca.silver.businesslogic.ids.ParameterID)
	 */
	public String getName(ParameterID pId, StorageI storage) throws Exception {
		return storage.getParameterName(pId.getIdVal());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#getType(edu.cs.usask
	 * .ca.silver.businesslogic.ids.ParameterID)
	 */
	public String getType(ParameterID pId, StorageI storage) throws Exception {
		return storage.getParameterType(pId.getIdVal());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#getUnits(edu.cs.usask
	 * .ca.silver.businesslogic.ids.ParameterID)
	 */
	public String getUnits(ParameterID pId, StorageI storage) throws Exception {
		return storage.getParameterUnits(pId.getIdVal());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#getAssociatedClass
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ParameterID)
	 */
	public String getAssociatedClass(ParameterID pId, StorageI storage) throws Exception {
		return storage.getParameterAssociatedClass(
				pId.getIdVal());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#getDescription(edu
	 * .cs.usask.ca.silver.businesslogic.ids.ParameterID)
	 */
	public String getDescription(ParameterID pId, StorageI storage) throws Exception {
		return storage.getParameterDescription(
				pId.getIdVal());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#setUnits(edu.cs.usask
	 * .ca.silver.businesslogic.ids.ParameterID, java.lang.String)
	 */
	public void setUnits(ParameterID pId, String units, StorageI storage) throws Exception {
		storage.setParameterUnits(pId.getIdVal(), units);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#setDescription(edu
	 * .cs.usask.ca.silver.businesslogic.ids.ParameterID, java.lang.String)
	 */
	public boolean setDescription(ParameterID pId, String description, StorageI storage)
			throws Exception {
		return storage.setParameterDescription(
				pId.getIdVal(), description);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#addParamteterToScenario
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ParameterID,
	 * edu.cs.usask.ca.silver.businesslogic.ids.ScenarioID, java.lang.String,
	 * java.lang.String)
	 */
	public void addParamteterToScenario(ParameterID pId, ScenarioID sId,
			String value, String comment, StorageI storage) throws Exception {
		boolean error = false;

		try {
			SilverBLTransactionManager.startTransaction();

			int psId = storage.getScenarioParameterSetId(
					sId.getUUIdVal());
			storage.addParameterToParameterSet(
					pId.getIdVal(), psId, value, comment);
		} catch (Exception e) {
			error = true;
			throw e;
		} finally {
			try {
				SilverBLTransactionManager.closeTransaction(error);
			} catch (Exception e) {
				throw e;
			}
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#deleteParameter(edu
	 * .cs.usask.ca.silver.businesslogic.ids.ParameterID)
	 */
	public boolean deleteParameter(ParameterID pId, StorageI storage) throws Exception {
		return storage.deleteParameter(pId.getIdVal());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ParameterI#getParentParameterSetId
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ParameterID)
	 */
	public ParameterSetID getParentParameterSetId(ParameterID pId, StorageI storage)
			throws Exception {
		return new ParameterSetID(storage
				.getParameterParentParameterSetId(pId.getIdVal()));
	}

}
